﻿db.system.js.save({
    _id: 'getProject',
    value: function (userId) {
        var objUserId = ObjectId(userId);
        var user = db.user.findOne({ _id: ObjectId(userId) }, { type: 1 });
        var where = {};
        if (user) {
            switch (user.type) {
                case 1:
                    where.student = objUserId;
                    break;
                case 2:
                    where.superVisor = objUserId;
                    break;
            }
        }
        return db.project.find(where).map(function (item) {
            item._id = item._id.valueOf();
            item.student = db.user.find({ _id: { $in: item.student } }, { pwd: 0 })
                                   .map(function (userItem) {
                                       userItem._id = userItem._id.valueOf();
                                       return userItem;
                                   });
            item.superVisor = db.user.find({ _id: { $in: item.superVisor } }, { pwd: 0 })
                                   .map(function (userItem) {
                                       userItem._id = userItem._id.valueOf();
                                       return userItem;
                                   });
            item.secondMarker = db.user.find({ _id: { $in: item.secondMarker } }, { pwd: 0 })
                                   .map(function (userItem) {
                                       userItem._id = userItem._id.valueOf();
                                       return userItem;
                                   });
            return item;
        });
    }
});

db.system.js.save({
    _id: 'createNewPost',
    value: function (fromUserId, toUserId, content) {
        var objFromUserId = ObjectId(fromUserId);
        var toUser = db.user.findOne({ _id: ObjectId(toUserId) }, { pwd: 0 }, { _id: 1 });
        db.post.insert({
            from: objFromUserId,
            to: toUser._id,
            content: content,
            comments: [],
            time: Date.now()
        });
        db.log.insert({
            agentId: fromUserId,
            type: 2,
            time: Date.now()
        });
    }
});

db.system.js.save({
    _id: 'getPost',
    value: function (userId) {
        var objUserId = ObjectId(userId);
        return db.post.find({
            $and: [
                {
                    content: {
                        $exists: true
                    }
                },
                {
                    $or: [
                        { to: objUserId },
                        { from: objUserId }
                    ]
                }
            ]
        }).map(function (item) {
            var users = {};
            for (var i = 0; i < item.comments.length; i++) {
                if (!users[item.comments[i].from]) {
                    users[item.comments[i].from] = db.user.findOne({ _id: item.comments[i].from }, { pwd: 0 });
                }
                item.comments[i].from = users[item.comments[i].from];
            }
            return item;
        });
    }
});

db.system.js.save({
    _id: 'pushComment',
    value: function (userId, messageId, content) {
        db.log.insert({
            agentId: userId,
            type: 3,
            time: Date.now()
        });
        return db.post.update({ _id: ObjectId(messageId) }, {
            $push: {
                comments: {
                    from: ObjectId(userId),
                    content: content,
                    time: Date.now()
                }
            }
        });
    }
});

db.system.js.save({
    _id: 'getProjects',
    value: function (userId) {
        var user = db.user.findOne({ _id: ObjectId(userId) }, { type: 1 });
        var where = {};
        if (user.type == 1) {
            where.student = user._id.valueOf();
        } else if (user.type == 4) {
            where.supervisor = user._id.valueOf();
        } else if (user.type == 3) {
            where.secondMarker = user._id.valueOf();
        } else if (user.type == 4) {

        }
        return db.project.find(where).map(function (item) {
            item._id = item._id.valueOf();
            item.studentName = '';
            item.superVisorName = '';
            item.secondMarkerName = '';
            if (item.student) {
                var student = db.user.findOne({ _id: ObjectId(item.student) }, { pwd: 0 });
                if (student) {
                    item.studentName = student.user;
                }
            }
            if (item.superVisor) {
                var superVisor = db.user.findOne({ _id: ObjectId(item.superVisor) }, { pwd: 0 });
                if (superVisor) {
                    item.superVisorName = superVisor.user;
                }
            }
            if (item.secondMarker) {
                var secondMarker = db.user.findOne({ _id: ObjectId(item.secondMarker) }, { pwd: 0 });
                if (secondMarker) {
                    item.secondMarkerName = secondMarker.user;
                }
            }
            return item;
        });
    }
});

db.system.js.save({
    _id: 'saveProject',
    value: function (userId, id, name, desc, file, student, supervisor, secondMarker) {
        var up = {
            name: name,
            desc: desc,
            file: file,

            student: student,
            supervisor: supervisor,
            secondMarker: secondMarker
        };

        var user = db.user.findOne({ _id: ObjectId(userId) }, { type: 1 });
        if (user.type == 1) {
            up.student = userId;
        } else if (user.type == 2) {
            delete up.supervisor;
        } else if (user.type == 3) {
            delete up.secondMarker;
        } else if (user.type == 4) {

        }

        if (!id) {
            db.project.insert(up);
        } else {
            db.project.update({ _id: ObjectId(id) }, up);
        }
    }
});


db.system.js.save({
    _id: 'getStatistic',
    value: function (userId) {
        var cursor = db.log.find({ "agentId": userId });
        var result = {
            totalAction: cursor.count()
        };
        cursor.forEach(function (item) {
            if (!result[item.type]) {
                result[item.type] = 0;
            }
            result[item.type]++;
        });
        result["totalLogin"] = result["1"];
        result["totalPost"] = result["2"];
        result["totalComment"] = result["3"];

        delete result["1"];
        delete result["2"];
        delete result["3"];
        return result;
    }
});